//
//  ShouCangViewController.m
//  XiangMu
//
//  Created by 康世标 on 15/8/12.
//  Copyright (c) 2015年 康世标. All rights reserved.
//

#import "ShouCangViewController.h"

@interface ShouCangViewController ()

@end

@implementation ShouCangViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     self.tabBarController.tabBar.hidden=YES;
    UIButton *fanHuiBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    fanHuiBtn.frame=CGRectMake(20, 20, 10, 20);
    
    [fanHuiBtn setBackgroundImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    
    [fanHuiBtn addTarget:self action:@selector(FanHui) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *barFanHui=[[UIBarButtonItem alloc]initWithCustomView:fanHuiBtn];
    self.navigationItem.leftBarButtonItem=barFanHui;
    self.dic = [[NSMutableDictionary alloc]initWithCapacity:0];
    
    [self.dic setObject:@(10) forKey:@"rows"];
    [self.dic setObject:[[NSUserDefaults standardUserDefaults]objectForKey:@"ID"] forKey:@"userId"];
    self.datasArr = [[NSMutableArray alloc]initWithCapacity:0];
    i=1;
    self.tableView.backgroundColor = CL_efeff4;
    self.tableView.separatorColor = CL_efeff4;

    [self jingWeiDu];
   
    [self QingQiuShuJU];
    // Do any additional setup after loading the view from its nib.
    [self mbpogress];
    // 下拉刷新
    [self.tableView addPullToRefreshWithActionHandler:^
     {
         
         [self performSelector:@selector(xialaSuaXin) withObject:self afterDelay:1.5];
         
     }];
    //上推加载更多
    [self.tableView addInfiniteScrollingWithActionHandler:^{
        
        [self performSelector:@selector(JiaZaiGengDuo) withObject:self afterDelay:1];
    }];
    
   
}
- (void)xialaSuaXin{
    [_datasArr removeAllObjects];
    
    i =1;
    
    [self QingQiuShuJU];
    
}
//上推加载更多
- (void)JiaZaiGengDuo{
    
    
    i++;
    if (i<=_msgPage)
    {
        
       
        
        [self QingQiuShuJU];
        
    }
    else{
        
        [self.tableView.infiniteScrollingView stopAnimating];
        
        [self.tableView.pullToRefreshView stopAnimating];
        
        hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        
        hud.mode=MBProgressHUDModeText;
        
        hud.labelText=@"没有更多数据了";
        
        hud.removeFromSuperViewOnHide=YES;
        
        [hud hide:YES afterDelay:1.5];
        
        
    }
    
}

- (void)mbpogress{
    
    hud =[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"数据加载中...";
    
    
}
- (void)QingQiuShuJU{
  
    [self.dic setObject:@(i) forKey:@"page"];
    [QingQiuShuJu getWodeShoucang:self.dic Block:^(NSDictionary *dic, BOOL isOK)
     {
       
         if (isOK) {
             
             if ([[dic valueForKey:@"status"] intValue]==200) {
                 
                 [self.datasArr addObjectsFromArray:[dic valueForKey:@"data"]];
                 self.msgPage = [[dic valueForKey:@"msg"] intValue];
                 [hud hide:YES];
                 [self.tableView.infiniteScrollingView stopAnimating];
                 
                 [self.tableView.pullToRefreshView stopAnimating];
                 [self.tableView reloadData];
             }
             else{
                 hud.mode = MBProgressHUDModeText;
                 
                 hud.labelText = [dic valueForKey:@"msg"];
                 [hud hide:YES afterDelay:2];
                 [self.tableView.infiniteScrollingView stopAnimating];
                 
                 [self.tableView.pullToRefreshView stopAnimating];
             }
         }
         else{
             
             hud.mode = MBProgressHUDModeText;
             hud.labelText = @"网络连接错误";
             [hud hide:YES afterDelay:2];
             [self.tableView.infiniteScrollingView stopAnimating];
             
             [self.tableView.pullToRefreshView stopAnimating];
         }
        
    }];
}

-(void)FanHui{
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden=NO;
}
#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.datasArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PeiHuShiTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"peihushi"];
    if (!cell) {
        cell=[[[NSBundle mainBundle]loadNibNamed:@"PeiHuShiTableViewCell" owner:nil options:nil]lastObject];
        
    }
    
    [cell shuJu:self.datasArr index:indexPath.row lat:self.latitude longitude:self.longitude];
    
    
    return cell;
    
}
#pragma mark -- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  
    return 116;

    
}
- (void)jingWeiDu{
    [[CCLocationManager shareLocation ] getLocationCoordinate:^(CLLocationCoordinate2D locationCorrrdinate) {
        
        _longitude=locationCorrrdinate.longitude;
        _latitude = locationCorrrdinate.latitude;
        if (_latitude ==0||_longitude ==0) {
            
            [[[UIAlertView alloc]initWithTitle:nil message:@"定位失败" delegate:nil cancelButtonTitle:nil otherButtonTitles: @"取消",nil]show];
            return ;
        }
        
        [self.tableView reloadData];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
